// pages/mine/editInfo/editInfo.js
import Common from '../../../common/common.js'
const common = new Common();
Page({
  data: {
    user: {
      birthday: '', //生日
      sex: 0, //1男 2女
      username: '', //姓名,
      userphone: '',
    },
    myInfo: {},
    editShow: false,
    eidtType: 0, //1性别 2手机号 3姓名 4生日
  },
  onLoad: function(options) {

  },
  onShow: function() {
    this.getMyInfo();
  },
  closeWindows() {
    this.setData({
      eidtType: 0,
      editShow: false,
      user: {}
    })
  },
  showWindows(e) {
    let type = e.currentTarget.dataset.type;
    this.setData({
      eidtType: type,
      editShow: true,
    })
  },
  //改变名称  
  changeName(e) {
    let username = 'user.username';
    this.setData({
      [username]: e.detail.value
    })
  },
  //改变性别
  radioSexChange(e) {
    let sex = 'user.sex';
    this.setData({
      [sex]: e.detail.value
    })
  },
  //改变手机号
  getPhoneNumber(e) {
    let phone = 'user.userphone';
    this.setData({
      [phone]: e.detail.value
    })
  },
  bindDateChange: function(e) {
    let birthday = 'user.birthday';
    this.setData({
      [birthday]: e.detail.value,
      eidtType: 4
    })
    this.eidtConfirm();
  },
  eidtConfirm() {
    let data = {
      memberId: wx.getStorageSync('memberId')
    }
    let type = this.data.eidtType;

    let canGoOn = true; //验证信息

    if (type == 1) {
      //性别
      data.sex = this.data.user.sex;
    } else if (type == 2) {
      //手机号
      data.cellPhoneNo = this.data.user.userphone;
      canGoOn = data.cellPhoneNo ? true : false;
    } else if (type == 3) {
      //姓名
      data.userName = this.data.user.username;
      canGoOn = data.userName ? true : false;
    } else if (type == 4) {
      //生日
      data.birthday = this.data.user.birthday;
    }
    if (!canGoOn){
      //验证不通过
      this.setData({
        editShow: false
      })
      return false
    }
    common.requestAjax('/api/member-service/v1/member/updateMemberDetail', 'POST', data).then(res => {
      if (res.data.code) {
        wx.showToast({
          title: '修改成功！',
          icon: 'success'
        })
        this.setData({
          editShow: false,
          user: {}
        })
        this.getMyInfo();
      } else {
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        })
      }
    })
  },

  /**
   * 退出
   */
  exitLogin(event) {},
  /**
   * 获取个人信息
   */
  getMyInfo() {
    let memberId = wx.getStorageSync('memberId');
    common.requestAjax('/api/member-service/v1/member/queryMemberDetail?memberId=' + memberId, 'POST', {}).then(res => {
      if (res.data.code) {
        this.setData({
          myInfo: res.data.records[0]
        })
      }
    })
  },
})